﻿using System;
using System.Collections.Generic;
using System.Text;
using TK3DSim;
using YY.Basic.DB;
using TK3DSim.Mil;
using System.Data;

namespace Tpro6Container.Mil
{
    public class ArmyBHs:TableRelateObj
    {
        public ArmyBHs()
            : base(Global.Instance.DB, "TBiaoHao", "ID")
        {

        }    /// <summary>
        /// 部队标号的数量
        /// </summary>
        public int Count
        {
            get { return this.Table.Rows.Count; }
        }

        /// <summary>
        /// 添加一个标号对象
        /// </summary>
        /// <param name="name">对象名称</param>
        /// <returns></returns>
        public ArmyBH Add(Organizations ArmyStep)
        {
            if (this.HaveExist(ArmyStep))
            {
                throw new Exception("该级别标号已经存在");
            }
            DataRow row = this.Table.NewRow();

            row["ID"] = System.Guid.NewGuid().ToString();
            row["编制"] = ArmyStep;
            row["filename"] = System.DBNull.Value;
            row["filescal"] = 1;
            row["maxv"] = System.DBNull.Value;
            row["minv"] = System.DBNull.Value;

            this.Table.Rows.Add(row);

            bool res = this.UpdateData();

            if (res)
            {
                return new ArmyBH(row);
            }
            else
            {
                throw new Exception("部队标号对象创建失败");
            }
        }

        /// <summary>
        /// 删除标号对象
        /// </summary>
        /// <param name="ID">标号对象的ID</param>
        public virtual void Delete(string ID)
        {
            DataRow[] rows = this.Table.Select("ID='" + ID + "'");
            if (rows.Length <= 0)
            {
                return;
            }
            else
            {
                //判断是否有部队已经配备了该武器装备，如果是则抛出异常，否则删除
                if (this.IsArmyUseThisArmament(ID))
                {
                    throw new Exception("有部队已经装备了ID号为:" + ID + "的武器装备，该装备信息无法删除");
                }
                else
                {
                    rows[0].Delete();
                    this.UpdateData();
                }
            }
        }

        public bool Exist(string ID)
        {
            DataRow[] rows = this.Table.Select("ID='" + ID + "'");
            if (rows.Length <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 获取标号对象
        /// </summary>
        /// <param name="ID">标号对象的ID</param>
        /// <returns></returns>
        public ArmyBH this[Organizations ArmyStep]
        {
            get
            {
                DataRow[] rows = this.Table.Select("编制='" + (int)ArmyStep + "'");
                if (rows.Length <= 0)
                {
                    throw new Exception("没有编制为:" + ArmyStep.ToString() + "的对象");
                }

                return new ArmyBH(rows[0]);
            }
        }
        /// <summary>
        /// 获取标号对象
        /// </summary>
        /// <param name="ID">标号对象的ID</param>
        /// <returns></returns>
        public ArmyBH this[string ID]
        {
            get
            {
                DataRow[] rows = this.Table.Select("ID='" + ID + "'");
                if (rows.Length <= 0)
                {
                    throw new Exception("Armaments 没有ID号为:" + ID + "的对象");
                }

                return new ArmyBH(rows[0]);
            }
        }

        /// <summary>
        /// 获取标号对象
        /// </summary>
        /// <param name="index">从0开始的索引</param>
        /// <returns></returns>
        public ArmyBH this[int index]
        {
            get
            {
                if (index >= this.Table.Rows.Count) { throw new Exception("Armaments 索引越界"); }
                else
                {
                    return new ArmyBH(this.Table.Rows[index]);
                }
            }
        }

        /// <summary>
        /// 复制一个标号对象
        /// </summary>
        /// <param name="ID">要复制对象的ID</param>
        /// <returns></returns>
        public ArmyBH Copy(string ID)
        {
            DataRow[] rows = this.Table.Select("ID='" + ID + "'");
            if (rows.Length <= 0)
            {
                throw new Exception("Armaments 没有ID号为:" + ID + "的对象");
            }

            ArmyBH oT = new ArmyBH(rows[0]);

            int i = 1;

            while (this.HaveExist(oT.Organization))
            {
                i++;
            }

            ArmyBH oNew = this.Add(oT.Organization);

            oNew.FileName = oT.FileName;
            oNew.FileScal = oT.FileScal;
            oNew.MaxV = oT.MaxV;
            oNew.MinV = oT.MinV;


            return oNew;
        }


        /// <summary>
        /// 判断是否已经有部队用了标号
        /// </summary>
        /// <param name="ID">标号对象的ID</param>
        /// <returns></returns>
        public bool IsArmyUseThisArmament(string WeaponID)
        {
            //string sSel = "Select Count(*) From TArmy Where 武器ID='" + WeaponID + "'";
            //DataTable tbl = Global.Instance.QueryObject.DB.SelectCommand(sSel);

            //if (Convert.ToInt32(tbl.Rows[0][0]) > 0)
            //{
            //    return true;
            //}
            //else
            //{
            //}
                return false;
        }

        /// <summary>
        /// 判断是否存在相同名称的编制
        /// </summary>
        /// <param name="name">编制</param>
        /// <returns></returns>
        public bool HaveExist(Organizations name)
        {
            DataRow[] rows = this.Table.Select("编制='" +(int) name + "'");
            if (rows.Length <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
